document.write("<script type='text/javascript' src='/lib/dc/request.js'></script>");
document.write("<script type='text/javascript' src='/lib/dc/util.js'></script>");
document.write("<script type='text/javascript' src='/lib/jquery.min.js'></script>");

class PageHelper {

    p_result;//Result对象
    p_limit = 2;//限制条数
    p_key = '';//关键词
    p_cur = 1;//当前页
    p_skip = 1;//跳页
    p_dels = [];//删除的id列表*/
    e_limit =''; //dom元素的id或者class，绑定限制页数
    e_total ='';//dom元素的id或者class，绑定所有条数
    e_skip ='';//dom元素的id或者class，绑定跳页
    e_select ='';//dom元素的id或者class，绑定单选
    e_select_all = '';//dom元素的id或者class，绑定全选

    constructor(e_limit,e_total,e_skip,e_select,e_select_all){
        this.e_limit = e_limit;
        this.e_total = e_total;
        this.e_skip = e_skip;
        this.e_select = e_select;
        this.e_select_all = e_select_all;
    }
    /*刷新页码*/

    refreshPage() {
        $(this.e_limit).text(this.p_result.limit);
        $(this.e_total).text(this.p_result.total);
        $(this.e_skip).val(this.p_result.page);
    }

    /*渲染列表函数，需要重写*/
    getInfo(){
        //todo
    }

   /*刷新当前页*/
    refreshData(){
        this.getInfo();
    }

    /*上一页*/
    lastPage() {
        if (this.p_cur <= 1){
            alert("当前为第一页");
            return;
        }
        this.p_cur --;
        this.getInfo();
        this.resetSelect();
    }

    /*下一页*/

    nextPage() {
        if (this.p_cur >= this.p_result.totalPage){
            alert("当前为最后一页");
            return;
        }
        this.p_cur ++;
        this.getInfo();
        this.resetSelect();
    }

    /*跳页*/

    skipPage(e) {
        var page = $(e).val();
        this.p_skip = page;
        this.p_cur =page;
        if (isNaN(this.p_skip)){
            alert("请正确输入页数");
            return;
        } else if (this.p_skip < 1){
            alert("页数不能小于1");
            return;
        }else if (this.p_skip > this.p_result.totalPage){
            alert("页数不能超过"+this.p_result.totalPage);
            return;
        }
        this.resetSelect();
        this.getInfo(this.p_skip);
    }

    /*搜索方法*/


    search(e) {
        this.p_cur = 1;
        this.p_key = $(e).val();
        this.getInfo();
    }

    /*选择一个checkbox*/
    selected(e) {
        var $p_select_all = $(this.e_select_all);
        var el = $(e);
        var check = el.prop('checked');
        var val = el.val();
        if (check)
            arrayPush(this.p_dels,val);
        else
            arrayPop(this.p_dels,val);
        if (this.p_dels.length >= this.p_result.data.length)
            $p_select_all.prop('checked',true);
        else
            $p_select_all.prop('checked',false);
        console.log(this.p_dels);
    }

    /*选择全部checkbox*/
    selectedAll(e) {
        var $p_select = $(this.e_select);
        var checked = $(e).prop('checked');
        if (checked){
            var list = this.p_result.data;
            for (var i = 0; i < list.length; i++) {
                this.arrayPush(this.p_dels,list[i].id);//item.id 根据实际字段
            }
            $p_select.each(function (i,e) {
                $(e).prop('checked',true);
            })
        }else{
            this.p_dels.splice(0);
            $p_select.each(function (i,e) {
                $(e).prop('checked',false);
            })
        }
        console.log(this.p_dels);
    }

    /*删除勾选*/
    delAllSelect(url) {
        if (confirm("确定要删除吗？")){
            POST(url,{ids:this.p_dels},function (data) {
                this.Alert(data);
                this.refreshData();
            })
        }
    }

    /*提醒框*/
    Alert(data) {
        alert(data.message);
    }

    /*重置选中状态*/
    resetSelect() {
        var $p_select = $(this.e_select_all);
        $p_select.prop('checked',false);
        this.p_dels.splice(0);
    }


    /**
     * 数组push去重
     * */
    arrayPush(targetArr,val){
        if (!isArray(targetArr))
            return;
        if (targetArr.indexOf(val) === -1)
            targetArr.push(val);
        return targetArr;
    }

    arrayPop(targetArr,val) {
        var index = -1;
        if (!isArray(targetArr))
            return;
        index = targetArr.indexOf(val);
        if ( index !== -1)
            targetArr.splice(index,1);
        return targetArr;
    }
}


